﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Helpers.cs" company="Scrum for Team System">
//   None
// </copyright>
// <summary>
//   Defines the Helpers type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Bootstrapper
{
    using System;
    using System.IO;
    using System.Reflection;
    using System.Text;
    
    internal class Helpers
    {
        internal static void ExtractEmbeddedResources(string ResourceName, string Filename)
        {
            // Unpack the embedded resource
            Assembly myAssembly = Assembly.GetExecutingAssembly();
            Stream res = myAssembly.GetManifestResourceStream(ResourceName);

            if (res == null)
            {
                throw new Exception(string.Format("Unable to locate the resource '{0}'.", ResourceName));
            }

            // Write the resource to the filesystem
            FileStream fout = new FileStream(Filename, FileMode.Create);
            try
            {
                int i = 0;
                do
                {
                    i = res.ReadByte();
                    if (i != -1)
                    {
                        fout.WriteByte((byte)i);
                    }
                }
                while (i != -1);
            }
            catch (Exception ex)
            {
                throw new Exception(
                                    string.Format("Unexpected error whilst writing resource '{0}' to the file '{1}'.", ResourceName, Filename),
                                    ex
                                    );
            }

            res.Close();
            fout.Close();
        }
    }
}
